<?php

namespace App\Repositories;

use App\Models\User;
use App\Models\Worker;

class WorkerRepository
{

    /**
     * 根据user_id获取员工
     *
     * @param int $user_id
     * @return mixed
     */
    public function getByUserId($user_id)
    {
        return Worker::where('user_id', $user_id)->first();
    }

    /**
     * 获取用户列表（分页）
     *
     * @param array $condition
     * @param array $with
     * @param string $order_by_column
     * @param string $order_by_direction
     * @param int $per_page
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function paginate($condition = [], $with = ['user'], $order_by_column = 'id', $order_by_direction = 'desc', $per_page = 20)
    {
        $user = Worker::with($with)->orderBy($order_by_column, $order_by_direction);
        if (isset($condition['enabled']) && is_numeric($condition['enabled'])) {
            $user->where('enabled', $condition['enabled']);
        }
        return $user->paginate($per_page);
    }
}
